<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
if (class_exists('PrunusLibPlugin')) {
  /**
   * PrunusBuffer
   *
   * Plugin for text buffer, consumed in other prunus plugins
   *  
   * @package Prunus   
   * @author agosto <agosto@7dedos.com>
   * @copyright	Copyright (c) 2009-2010, 7dedos
   * @link http://www.7dedos.com
   * @version 0.3
   * @access public
   */
  class PrunusBuffer extends PrunusLibPlugin {
    private $buffer;
    private $offsetPosition;
  
    /**
     * PrunusBuffer::__construct()
     * 
     * @param integer $offset Initializes the offset position 
     * @return
     */
    public function __construct($offset = 0)	{
      parent::__construct();
      $this->version='0.3';
      $this->name='PrunusBuffer Plugin';
      $this->path = strtolower(@realpath(dirname(__FILE__)));
      $this->file = strtolower(@pathinfo(__FILE__, PATHINFO_BASENAME));
      
      $this->offsetPosition = $offset;
      $this->buffer = '';
      log_message('debug', 'PrunusBuffer inicializado');
    }
    
    /**
     * PrunusBuffer::__destruct()
     * 
     * @return
     */
    public function __destruct ()	{
      log_message('debug', 'PrunusBuffer destruida');
    }
    
    /**
     * PrunusBuffer::clear()
     * 
     * Empty the buffer
     * 
     * @param integer|null $offset re-initialize the offset position (use null to make no change to the offset value)
     * @return
     */
    public function clear($offset = null) { 
      $this->buffer = ''; 
      if ($offset !== null) $this->offsetPosition = $offset;
    }
    
    /**
     * PrunusBuffer::addLine()
     * 
     * Add a new line to the buffer
     * 
     * @param integer $position offset for this very line. it's added to the buffer offset 
     * @param string $text line to add
     * @return
     */
    public function addLine($position, $text) { 
      $this->buffer .= $this->ci->prunuslib->gi(($this->offsetPosition + $position), $text); 
    }
    
    /**
     * PrunusBuffer::get()
     * 
     * Returns the buffer content
     * 
     * @return string 
     */
    public function get() {
      return $this->buffer;
    }
  
  }
  
}
